/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.define.ssh.util;

import java.util.List;

import org.apache.poi.ss.formula.functions.T;

/**
 * 
 * @author agu
 * 
 */
public class PageSupport {

	private int pageSize = 20; // 每页显示记录数,默认为20
	private int pageNo = 1; // 当前的页码
	private int pageCount = 0; // 总页数
	private int recordCount; // 总记录数
	private List<T> items; // 当前页的存储对象
	/**
	 * 起始页
	 */
	private int startpage;
	/**
	 * 结束页
	 */
	private int endpage;

	public PageSupport() {
		this.pageNo = 1;

	}

	/**
	 * 这个方法由张如兵添加 为何增加这个方法呢 这样调用参数最少, 使用更方便 需要的请求分页的信息已经在page里面了
	 * 
	 * @param page
	 * @param items
	 * @param recordCount
	 */
	public PageSupport(PageSupport page, List<T> items, int recordCount) {
		this.items = items;
		this.pageSize = page.getPageSize();
		this.pageNo = page.getPageNo();
		this.setRecordCount(recordCount);

		this.startpage = pageNo - 2;
		if (startpage < 1) {
			startpage = 1;
		}
		this.endpage = startpage + 4;
		endpage = startpage + 4;
		if (endpage > pageCount) {
			endpage = pageCount;
		}
	}

	/**
	 * 根据记录数来计算页数 前提是相关数据已经在对象中.
	 * 
	 * @param recordCount
	 */
	public void setRecordCount(int recordCount) {
		this.recordCount = recordCount;
		pageCount = (recordCount - 1) / pageSize + 1;
		pageNo = pageNo > pageCount ? pageCount : pageNo;
		this.startpage = pageNo - 2;
		if (startpage < 1) {
			startpage = 1;
		}
		endpage = startpage + 4;
		if (endpage > pageCount) {
			endpage = pageCount;
		}
	}

	/**
	 * 取开始记录
	 * 
	 * @return
	 */
	public int getStartRecord() {
		return pageSize * (pageNo - 1);
	}

	/**
	 * 
	 * @param items
	 *            存储的对象
	 * @param recordCount
	 *            总记录数
	 * @param pageSize
	 *            当前页显示的个数
	 * @param pageNo
	 *            当前页码
	 */
	public PageSupport(List<T> items, int recordCount, int pageSize, int pageNo) {
		this.pageNo = pageNo;
		this.items = items;
		this.pageSize = pageSize;
		if (recordCount > 0) {
			this.recordCount = recordCount;
			this.pageCount = (recordCount - 1) / pageSize + 1;
		}
		this.startpage = pageNo - 2;
		if (startpage < 1) {
			startpage = 1;
		}
		endpage = startpage + 4;
		if (endpage > pageCount) {
			endpage = pageCount;
		}
	}

	/**
	 * 
	 * @return
	 */
	public List getItems() {
		return items;
	}

	public void setItems(List items) {
		this.items = items;
	}

	/**
	 * 
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 
	 * @return
	 */
	public int getPageNo() {
		return this.pageNo;
	}

	/**
	 * 
	 * @return
	 */
	public int getPageCount() {
		return pageCount;
	}

	/**
	 * 
	 * @return
	 */
	public int getRecordCount() {
		return recordCount;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getStartpage() {
		return startpage;
	}

	public int getEndpage() {
		return endpage;
	}

	public void setStartpage(int startpage) {
		this.startpage = startpage;
	}

	public void setEndpage(int endpage) {
		this.endpage = endpage;
	}

}